#!/bin/bash
set -e

git_status_short=$(git status -s)

if [ ! $git_status_short ]; then

  read -p "是否执行 ttm d ? (y/n) " -n 1 -r reply
  echo
  if [[ $reply =~ ^[Yy]$ ]]; then
     echo "开始编译"
     ttm d
     echo "编译完成"
  fi

  log=$(git log -1 --pretty=format:"%s --change by %an")

  author=$(git log -1 --pretty=format:"%an")

  if [[ -z $1 ]]; then
    read -p "git commit -m ${log} - are you sure? (y/n) " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Nn]$ ]]; then
      echo "Enter commit message: "
      read -r message
      log="$message --change by $author"
    fi
  else
    log="$1 --change by $author"
  fi

  get_branch=$(git symbolic-ref --short -q HEAD)

  git checkout release

  git pull origin release

  ls -a |grep -vE "^\.|^dist|^node_modules|^bash\.sh"|xargs rm -r

  cp -rf ./dist/* .

  git add .

  git commit -m "${log}" || true

  git push origin release

  git checkout "${get_branch}" || master

else

  echo "当前分支存在变动 请先提交"

fi
